package com.huaxin.hxenum.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.huaxin.hxenum.base.BaseCodeEnum;
import com.huaxin.hxenum.base.Describable;

/**
 * 数据类型枚举（用于采集项）
 */
public enum EquipmentDataType implements BaseCodeEnum<String>, Describable {

    // ========== 电压类 ==========
    VOLTAGE_PHASE_A("voltage_a_kv", "A相电压", DataType.NUMBER, null, Unit.KILO_VOLT, "A相相电压，单位：kV"),
    VOLTAGE_PHASE_B("voltage_b_kv", "B相电压", DataType.NUMBER, null, Unit.KILO_VOLT, "B相相电压，单位：kV"),
    VOLTAGE_PHASE_C("voltage_c_kv", "C相电压", DataType.NUMBER, null, Unit.KILO_VOLT, "C相相电压，单位：kV"),
    VOLTAGE_LINE_AB("voltage_ab_kv", "AB线电压", DataType.NUMBER, null, Unit.KILO_VOLT, "AB线电压，单位：kV"),
    VOLTAGE_LINE_BC("voltage_bc_kv", "BC线电压", DataType.NUMBER, null, Unit.KILO_VOLT, "BC线电压，单位：kV"),
    VOLTAGE_LINE_CA("voltage_ca_kv", "CA线电压", DataType.NUMBER, null, Unit.KILO_VOLT, "CA线电压，单位：kV"),

    // ========== 电流类 ==========
    CURRENT_PHASE_A("current_a_a", "A相电流", DataType.NUMBER, null, Unit.AMPERE, "A相电流，单位：A"),
    CURRENT_PHASE_B("current_b_a", "B相电流", DataType.NUMBER, null, Unit.AMPERE, "B相电流，单位：A"),
    CURRENT_PHASE_C("current_c_a", "C相电流", DataType.NUMBER, null, Unit.AMPERE, "C相电流，单位：A"),

    // ========== 功率类 ==========
    POWER_FACTOR_TOTAL("power_factor_total", "总功率因数", DataType.NUMBER, null, null, "三相总功率因数，范围：0~1"),
    ACTIVE_POWER_TOTAL("active_power_total_kw", "总有功功率", DataType.NUMBER, null, Unit.KILO_WATT, "三相总有功功率，单位：kW"),
    REACTIVE_POWER_TOTAL("reactive_power_total_kvar", "总无功功率", DataType.NUMBER, null, Unit.KILO_VAR, "三相总无功功率，单位：kvar"),
    APPARENT_POWER_TOTAL("apparent_power_total_kva", "总视在功率", DataType.NUMBER, null, Unit.KILO_VA, "三相总视在功率，单位：kVA"),

    // ========== 电能类 ==========
    ENERGY_ACTIVE_FORWARD("energy_active_forward_kwh", "正向有功电度", DataType.NUMBER, null, Unit.KILO_WATT_HOUR, "累计正向有功电度，单位：kWh"),
    ENERGY_REACTIVE_FORWARD("energy_reactive_forward_kwh", "正向无功电度", DataType.NUMBER, null, Unit.KILO_WATT_HOUR, "累计正向无功电度，单位：kWh"),
    ENERGY_ACTIVE_REVERSE("energy_active_reverse_kwh", "反向有功电度", DataType.NUMBER, null, Unit.KILO_WATT_HOUR, "累计反向有功电度，单位：kWh"),
    ENERGY_REACTIVE_REVERSE("energy_reactive_reverse_kwh", "反向无功电度", DataType.NUMBER, null, Unit.KILO_WATT_HOUR, "累计反向无功电度，单位：kWh"),

    // ========== 频率 ==========
    FREQUENCY("frequency_hz", "频率", DataType.NUMBER, null, Unit.HERTZ, "交流电系统频率，单位：Hz"),

    // ========== 告警类 ==========
    ALARM_SMOKE("alarm_smoke", "烟雾告警", DataType.BOOLEAN, null, null, "烟雾传感器状态：0正常，1告警"),
    ALARM_INTRUSION("alarm_intrusion", "人员入侵告警", DataType.BOOLEAN, null, null, "红外/摄像头检测到人员入侵：0正常，1告警"),
    ALARM_WATER("alarm_water", "水浸告警", DataType.BOOLEAN, null, null, "水浸传感器状态：0正常，1告警"),

    // ========== 环境类 ==========
    TEMPERATURE_ROOM("temperature_room", "环境温度", DataType.BOOLEAN, null, Unit.CELSIUS, "配电室/柜内环境温度，单位：℃"),
    HUMIDITY_ROOM("humidity_room_rh", "环境湿度", DataType.BOOLEAN, null, Unit.PERCENT, "配电室/柜内相对湿度，单位：%RH"),

    // ========== 断路器状态 ==========
    BREAKER_STATUS_1("breaker_1_status", "断路器1状态", DataType.ENUM, BreakerStatus.class.getSimpleName(), null, "断路器1分合闸状态：open=分闸，closed=合闸"),
    BREAKER_STATUS_2("breaker_2_status", "断路器2状态", DataType.ENUM, BreakerStatus.class.getSimpleName(), null, "断路器2分合闸状态：open=分闸，closed=合闸"),
    BREAKER_STATUS_3("breaker_3_status", "断路器3状态", DataType.ENUM, BreakerStatus.class.getSimpleName(), null, "断路器3分合闸状态：open=分闸，closed=合闸"),

    // ========== 设备温度类 ==========
    TEMPERATURE_TRANSFORMER_A("transformer_temp_a", "变压器A相温度", DataType.NUMBER, null, Unit.CELSIUS, "变压器A相绕组温度，单位：℃"),
    TEMPERATURE_TRANSFORMER_B("transformer_temp_b", "变压器B相温度", DataType.NUMBER, null, Unit.CELSIUS, "变压器B相绕组温度，单位：℃"),
    TEMPERATURE_TRANSFORMER_C("transformer_temp_c", "变压器C相温度", DataType.NUMBER, null, Unit.CELSIUS, "变压器C相绕组温度，单位：℃"),
    TEMPERATURE_CABLE_A("cable_temp_a", "电缆A相温度", DataType.NUMBER, null, Unit.CELSIUS, "电缆A相接头温度，单位：℃"),
    TEMPERATURE_CABLE_B("cable_temp_b", "电缆B相温度", DataType.NUMBER, null, Unit.CELSIUS, "电缆B相接头温度，单位：℃"),
    TEMPERATURE_CABLE_C("cable_temp_c", "电缆C相温度", DataType.NUMBER, null, Unit.CELSIUS, "电缆C相接头温度，单位：℃"),

    // ========== 视频快照 ==========
    VIDEO_SNAPSHOT("video_snapshot_url", "视频快照", DataType.STRING, null, Unit.NONE, "巡检时拍摄的视频帧，可能是URL或Base64编码");

    @EnumValue
    private final String code;
    private final String displayName;
    private final DataType dataType;
    private final String enumClassName;
    private final Unit unit;
    private final String description;

    EquipmentDataType(String code, String displayName, DataType dataType, String enumClassName, Unit unit, String description) {
        this.code = code;
        this.displayName = displayName;
        this.dataType = dataType;
        this.enumClassName = enumClassName;
        this.unit = unit;
        this.description = description;
    }

    @JsonValue
    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    public DataType getDataType() {
        return dataType;
    }

    public String getEnumClassName() {
        return enumClassName;
    }

    /**
     * 获取该数据类型对应的单位
     */
    public Unit getUnit() {
        return unit;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 根据 code 获取枚举实例
     */
    public static EquipmentDataType of(String code) {
        for (EquipmentDataType type : values()) {
            if (type.getCode().equalsIgnoreCase(code)) {
                return type;
            }
        }
        return null;
    }

    /**
     * 判断 code 是否有效
     */
    public static boolean isValid(String code) {
        return of(code) != null;
    }
}